home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / ov-complex.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  3KB  |  166 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "lo-ieee.h"
  32.  
  33. #include "oct-obj.h"
  34. #include "ops.h"
  35. #include "ov-complex.h"
  36. #include "ov-cx-mat.h"
  37. #include "ov-scalar.h"
  38. #include "gripes.h"
  39. #include "pr-output.h"
  40.  
  41. octave_allocator
  42. octave_complex::allocator (sizeof (octave_complex));
  43.  
  44. int
  45. octave_complex::t_id (-1);
  46.  
  47. const string
  48. octave_complex::t_name ("complex scalar");
  49.  
  50. octave_value *
  51. octave_complex::try_narrowing_conversion (void)
  52. {
  53.   octave_value *retval = 0;
  54.  
  55.   if (imag (scalar) == 0.0)
  56.     retval = new octave_scalar (::real (scalar));
  57.  
  58.   return retval;
  59. }
  60.  
  61. static inline bool
  62. valid_scalar_indices (const octave_value_list& args)
  63. {
  64.   int nargin = args.length ();
  65.  
  66.   for (int i = 0; i < nargin; i++)
  67.     if (! args(i).valid_as_scalar_index ())
  68.       return false;
  69.  
  70.   return true;
  71. }
  72.  
  73. octave_value
  74. octave_complex::index (const octave_value_list& idx) const
  75. {
  76.   octave_value retval;
  77.  
  78.   if (valid_scalar_indices (idx))
  79.     retval = scalar;
  80.   else
  81.     {
  82.       // XXX FIXME XXX -- this doesn't solve the problem of
  83.       //
  84.       //   a = i; a([1,1], [1,1], [1,1])
  85.       //
  86.       // and similar constructions.  Hmm...
  87.  
  88.       // XXX FIXME XXX -- using this constructor avoids narrowing the
  89.       // 1x1 matrix back to a scalar value.  Need a better solution
  90.       // to this problem.
  91.  
  92.       octave_value tmp (new octave_complex_matrix (complex_matrix_value ()));
  93.  
  94.       retval = tmp.index (idx);
  95.     }
  96.  
  97.   return retval;
  98. }
  99.  
  100. double
  101. octave_complex::double_value (bool force_conversion) const
  102. {
  103.   double retval = octave_NaN;
  104.  
  105.   int flag = force_conversion;
  106.  
  107.   if (! flag)
  108.     flag = Vok_to_lose_imaginary_part;
  109.  
  110.   if (flag < 0)
  111.     gripe_implicit_conversion ("complex scalar", "real scalar");
  112.  
  113.   if (flag)
  114.     retval = ::real (scalar);
  115.   else
  116.     gripe_invalid_conversion ("complex scalar", "real scalar");
  117.  
  118.   return retval;
  119. }
  120.  
  121. Matrix
  122. octave_complex::matrix_value (bool force_conversion) const
  123. {
  124.   Matrix retval;
  125.  
  126.   int flag = force_conversion;
  127.  
  128.   if (! flag)
  129.     flag = Vok_to_lose_imaginary_part;
  130.  
  131.   if (flag < 0)
  132.     gripe_implicit_conversion ("complex scalar", "real matrix");
  133.  
  134.   if (flag)
  135.     retval = Matrix (1, 1, ::real (scalar));
  136.   else
  137.     gripe_invalid_conversion ("complex scalar", "real matrix");
  138.  
  139.   return retval;
  140. }
  141.  
  142. Complex
  143. octave_complex::complex_value (bool) const
  144. {
  145.   return scalar;
  146. }
  147.  
  148.  
  149. ComplexMatrix
  150. octave_complex::complex_matrix_value (bool) const
  151. {
  152.   return ComplexMatrix (1, 1, scalar);
  153. }
  154.  
  155. void
  156. octave_complex::print (ostream& os, bool pr_as_read_syntax)
  157. {
  158.   octave_print_internal (os, scalar, pr_as_read_syntax);
  159. }
  160.  
  161. /*
  162. ;;; Local Variables: ***
  163. ;;; mode: C++ ***
  164. ;;; End: ***
  165. */
  166.